//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 潇 on 15/12/14.
//  Copyright © 2015年 潇. All rights reserved.
//

import UIKit

//定义协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法
    //用户将要登录
    //optional   可选的  登录的方法实现无所谓  但是不是想会爆炸
    func userWillLogin()
    
    //用户将要注册
    func userWillRegister()
}

class VisitorLoginView: UIView {
    
    //声明代理 代理要使用 weak 属性关键字
    //Swift 属性默认都是 strong  代理同样也需要使用 weak
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    //对外提供设置访客视图信息的方法
    func setupInfo(tipText: String, imageName: String?) {
    
        tipLabel.text = tipText
        if let name = imageName {
            //不是首页
            circleView.image = UIImage(named: name)
            //隐藏大房子
            largeIcon.hidden = true
            //将 circleView 移动到视图最顶层
            bringSubviewToFront(circleView)
        }else {
            //是首页,就开始做动画
            startAnimation()
        } 
        
    }
    //circleView动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT  //重复次数
        anim.duration = 20.0         //动画时间
        anim.toValue = 2 * M_PI      //转动
        //当动画完成后或在页面失去活跃状态 动画不移除图层
        anim.removedOnCompletion = false
        //添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
    }

    override init(frame: CGRect) {
    //调用 super.init()
        super.init(frame: frame)
        //添加视图 运行视图
        setupUI()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置所有的子视图
    private func setupUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(loginBtn)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        //添加了视图不到位,使用自动布局
        //NSLayoutConstraint  VFL
        
        /*
        item: <#T##AnyObject#>  需要俩接约束的对象
        attribute: <#T##NSLayoutAttribute#>添加约束的对象需要约束的属性
        relatedBy: <#T##NSLayoutRelation#>约束的关系  一般使用 equal 表示精准约束
        toItem: <#T##AnyObject?#>相对约束的对象
        attribute: <#T##NSLayoutAttribute#>相对约束的对象的属性
        multiplier: <#T##CGFloat#>乘积值  一般为1
        constant: <#T##CGFloat#>约束值
       */
        
        //约束的对象需要添加父视图 addConstraint
        //在使用手码添加约束的时候,需要屏蔽 frame 布局
//        largeIcon.translatesAutoresizingMaskIntoConstraints = false
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        //小房子的frame
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        // 圆环的 frame
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //tipLabel
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(item:  loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom , multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //backView
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
     //设置背景视图的约束 可视化的布局语言
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
        //设置视图的背景色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    //按钮的监听事件
    @objc private func registerBtnDidClick() {
        //代理调用协议方法  如果为 nil 就不会执行方法
        visitorViewDelegate?.userWillRegister()
    }
    
    @objc private func loginBtnDidClick() {
        //判断代理是否为空 && 是否响应方法  userWillLogin? 表示的是判断是否可以响应方法
        //加了 ! 表示这个方法一定可以实现
        visitorViewDelegate?.userWillLogin()
    }
    
    
    //调用了这里之后
    //懒加载所有的子视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回这里看看有什么惊喜.关注一些人,回这里看看有什么惊喜.", color:  UIColor.darkGrayColor(), fontSize: 14)
    
//    
//    {
//        let l = UILabel()
//        l.text = "关注一些人,回这里看看有什么惊喜.关注一些人,回这里看看有什么惊喜."
//        l.textColor = UIColor.darkGrayColor()   //字色
//        l.textAlignment = .Center   //居中
//        l.font = UIFont.systemFontOfSize(14) //字体
//        l.numberOfLines = 0   //换行
//        l.sizeToFit()   //更新
//        
//        return l
//    }()
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", fontSize: 15, color: themeColor)
    
//    {
//        let btn = UIButton()
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitle("注册", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
//        btn.setTitleColor(themeColor, forState:.Normal)
//        btn.sizeToFit()
//        
//        return btn
//    }()
    
    private lazy var loginBtn: UIButton = UIButton(title: "登录", backImageName: "common_button_white_disable", fontSize: 15, color: UIColor.darkGrayColor())
    
//    {
//    
//        let btn = UIButton()
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitle("登录", forState: . Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.sizeToFit()
//        
//        return btn
//    }()
}
